package com.example.springblog.model;

import com.example.springblog.constant.Constants;
import lombok.Data;

@Data
public class Result<T> {
    private int code;
    private T data;
    private String msg;

    // 业务处理成功，返回数据
    public static <T> Result success(T data) {
        Result result = new Result();
        result.setCode(Constants.RESULT_CODE_SUCCESS);
        result.setData(data);
        result.setMsg("");
        return result;
    }

    // 业务处理失败，返回错误信息
    public static <T> Result fail(String msg) {
        Result result = new Result();
        result.setCode(Constants.RESULT_CODE_FAIL);
        result.setMsg(msg);
        return result;
    }

    // 业务处理失败，返回错误信息和数据
    public static <T> Result fail(String msg, T data) {
        Result result = new Result();
        result.setCode(Constants.RESULT_CODE_FAIL);
        result.setMsg(msg);
        result.setData(data);
        return result;
    }

    // 用户未登录，返回未登录信息
    public static <T> Result unlogin(String msg) {
        Result result = new Result();
        result.setCode(Constants.RESULT_CODE_UNLOGIN);
        result.setMsg("用户未登录");
        return result;
    }
}
